package com.example.gravitysituation;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class CatapultTutorial extends Activity {

	
	// Tutorial parameters
		int numPages = 3;
		int currentPage;
		
		@Override
		// Set first page of tutorial as active view
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_catapult_tutorial);
			
			currentPage = 1;
			
			// Show first page
			ImageView imgView1 = (ImageView)findViewById(R.id.catapult_tutorial1);
			imgView1.setVisibility(View.VISIBLE);

			// Hide remaining pages
			for (int i = 2; i <=numPages; i++){
				String imageViewID = "catapult_tutorial" + i;
				int resID = getResources().getIdentifier(imageViewID, "id", "com.example.gravitysituation");
				ImageView imgView2 = (ImageView)findViewById(resID);
				imgView2 .setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			getMenuInflater().inflate(R.menu.catapult_tutorial, menu);
			return true;
		}
		
		public void runSimulation( View view ) {
	    	Intent intent = new Intent( this, CatapultSimulation.class);
	    	startActivity(intent);
	    }
		
		// Variables for swipe input
		float x1,x2;
	    float y1,y2;
		    
	    
	    @Override	   
	    // onTouchEvent () method gets called when User performs any touch event on screen 
	    // Method to handle swipe event
	    public boolean onTouchEvent(MotionEvent touchevent) {
	         switch (touchevent.getAction()) {
	            // when user first touches the screen, get x and y coordinate
	             case MotionEvent.ACTION_DOWN: 
	                 x1 = touchevent.getX();
	                 y1 = touchevent.getY();
	                 break;
	               
	             // user releases screen    
		         case MotionEvent.ACTION_UP: 
		        	 x2 = touchevent.getX();
		        	 y2 = touchevent.getY(); 
		        	 
		        	 String imageViewIDCurrent;
		        	 String imageViewIDNext;
		        	 int resIDCurrent;
		        	 int resIDNext;
		        	 int nextPage;
		        	 ImageView imgView1;
		        	 ImageView imgView2;
		        	 
		        	 

	                 
		        	 // if left to right sweep motion	
	                 if (x1 < x2) {
	                	 if(currentPage > 1){
	                		 // Set current page invisible
	                    	 imageViewIDCurrent = "catapult_tutorial" + currentPage;
	                    	 resIDCurrent = getResources().getIdentifier(imageViewIDCurrent, "id", "com.example.gravitysituation");
	                    	 imgView1 = (ImageView)findViewById(resIDCurrent);
	                         imgView1 .setVisibility(View.INVISIBLE);
	                         
	                         // Display next page
	                		 nextPage = currentPage - 1;
		                     imageViewIDNext = "catapult_tutorial" + nextPage;
		                	 resIDNext = getResources().getIdentifier(imageViewIDNext, "id", "com.example.gravitysituation");
		                	 imgView2 = (ImageView)findViewById(resIDNext);
		                     imgView2 .setVisibility(View.VISIBLE);
		                     currentPage = nextPage;
	                	 }
	                  }
	                
	                 // if right to left sweep event on screen display next image
	                 if (x1 > x2){
	                	 if(currentPage < numPages){
	                		 // Set current page invisible
	                    	 imageViewIDCurrent = "catapult_tutorial" + currentPage;
	                    	 resIDCurrent = getResources().getIdentifier(imageViewIDCurrent, "id", "com.example.gravitysituation");
	                    	 imgView1 = (ImageView)findViewById(resIDCurrent);
	                         imgView1 .setVisibility(View.INVISIBLE);
	                         
	                         // Display next page
	                		 nextPage = currentPage + 1;
		                     imageViewIDNext = "catapult_tutorial" + nextPage;
		                	 resIDNext = getResources().getIdentifier(imageViewIDNext, "id", "com.example.gravitysituation");
		                	 imgView2 = (ImageView)findViewById(resIDNext);
		                     imgView2 .setVisibility(View.VISIBLE);
		                     currentPage = nextPage;
	                	 }
	                 }            
	              
	                 break;
	                 
	         }
	         return false;
	    }
	}
